package controllers;

import java.beans.PropertyDescriptor;
import java.io.File;
import java.lang.reflect.InvocationTargetException;
import java.util.Collection;
import java.util.List;

import models.LatoCatalog;
import models.LatoTemplate;
import models.SysField;
import models.SysTable;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.beanutils.DynaBean;
import org.apache.commons.beanutils.LazyDynaBean;
import org.apache.commons.beanutils.PropertyUtils;
import org.apache.commons.fileupload.FileUploadException;

import com.sun.org.apache.xml.internal.resolver.Catalog;

import play.Logger;
import play.db.jpa.Blob;
import play.libs.Files;
import play.mvc.Controller;
import utils.JsonHelper;
/**
 * This is the Category interface use REST to communicate the client. 
 * <p>
 * Category interface:
 * <ul>
 *  <li><code>String getCatalogs(String userId);</code>
 *  <p>route: GET /catalogs/{userId}
 *  <p>test URL: <a href="http://localhost:9000/catalogs/1">http://localhost:9000/catalogs/1</a>
 *  <p>get a catalog list by userId 
 *  
 *  <li><code>String deleteCatalogs(String catalogsId);</code>
 *  <p>route: POST     /delcatalog/{catalogId}
 *  <p style='border:1px solid red;'>test Form:<form action="/delcatalog" method="post"><input type='text' name='catalogId' value='catalogId'>
 *  <input type='submit' name='catalogId' value='delete'></form></p>
 *  <p>delete the catalog, if return string length=0 means delete successfully, if return string length>0 means delete fails and string is the err msg.
 *  <li><code>String saveCatalog(String catalogJson);</code>
 *  <p>route: POST    /catalog
 *  <p style='border:1px solid red;'>test Form:<form>catalogJson:<input type='text' name='catalogJson' value='catalogId'>
 *  <input type='submit' value='save'></form></p>
 *  <p>if categoryId>0 means update catalog, otherwise will create catalog.
 *  </ul>
 *  
 * 
 *  
 * @author Liura
 * @since 2011-4-27
 */

public class LatoCatalogDelegate extends Controller{	
	/**
	 * get the catalog list json
	 * @return  catalog list json
	 */
	public static String getCatalogs(String userId) {
		String str = "[";
		List<LatoCatalog> catList = LatoCatalog.findAll();
		for (LatoCatalog latoCatalog : catList) {			
			DynaBean bean = new LazyDynaBean();
			bean.set("id", latoCatalog.id);
			bean.set("caption", latoCatalog.catalogName);
			bean.set("parentId", latoCatalog.parentId);
			bean.set("index", latoCatalog.childIndex);
			String beanJson = JsonHelper.getJsonFromBean(bean).toString();			
			str +=beanJson+",";
		}
		str = str.substring(0, str.length()-1);
		str +="]";
		Logger.info("bean list:"+str);
		return str;
	}
	
	/**
	 * delete catalog  by catalogId
	 */	
	public static String deleteCatalog(Long catalogId) {
		String errMsg="";
		try {
			LatoCatalog cat = LatoCatalog.findById(catalogId);
			cat.delete();
			Logger.info("delete Catalog... catalogId:"+catalogId);
		} catch (Exception e) {			
			e.printStackTrace();
			errMsg = e.toString();
		}
		return errMsg;
	}
	
	/**
	 * save catalog   
	 */	
	public static String saveCatalog(String catalogJson) {
		String str = "{name:\"saveCatalog\",array:"+catalogJson+"}";
		Logger.info("new json:"+str);
		JSONObject jsonObject = JSONObject.fromObject( str ); 
		Collection beanList = JSONArray.toCollection( jsonObject.getJSONArray( "array" ) );
		Logger.info("saveCatalog...size:"+beanList.size());
		for (Object o : beanList) {
			DynaBean bean = (DynaBean)o;
			Integer id = (Integer) bean.get("ID");
			if(id>0){
				LatoCatalog cat = LatoCatalog.findById(Long.valueOf(id));
				cat.catalogName = (String) bean.get("Caption");
				cat.parentId = (String) bean.get("ParentID");
				cat.childIndex = (String) bean.get("Index");
				cat.save();
				Logger.info("update cat:"+bean);
			}else{
				LatoCatalog cat = new LatoCatalog();
				cat.catalogName = (String) bean.get("Caption");
				cat.parentId = (String) bean.get("ParentID");
				cat.childIndex = (String) bean.get("Index");
				cat.save();
				Logger.info("create cat:"+bean);
			}
		}
		
		return "saveCatalog json";
	}

}
